﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyFavorites.Model;
using System.Globalization;

/// <summary>
///InitSystemDate 的摘要说明
/// </summary>
public static class InitSystemData
{
    public static void InitDate()
    {
        using (Entities entitie = new Entities())
        {
            AddPopuplstDataSex();
            AddPopuplstDataLanguage();
            AddPopuplstTarget();
            AddPopuplstShowType();

            AddDefaultUser();
            C_User DefaultUser = entitie.C_User.First(u => u.UserName == "Default");
            AddDefaultUserInfo(DefaultUser);
            AddDefaultCategory(DefaultUser);
        }
    }

    /// <summary>
    /// 性别
    /// </summary>
    public static void AddPopuplstDataSex()
    { 
        using (Entities entite = new Entities())
        {
            //性别
            C_Popuplst SexPopuplst = new C_Popuplst();
            SexPopuplst.CreateDate = DateTime.Now;
            SexPopuplst.Description = "性别";
            SexPopuplst.DisplayOrder = 1;
            SexPopuplst.IsSystem = true;
            SexPopuplst.ModifyDate = DateTime.Now;
            SexPopuplst.PopuplstType = "Sex";
            SexPopuplst.RecordStatus = "Active";
            SexPopuplst.StringCode = "Popuplst_Sex";
            entite.C_Popuplst.AddObject(SexPopuplst);
            //男性Male
            C_PopupLstItem SexPopupLstItemMale = new C_PopupLstItem();
            SexPopupLstItemMale.CreateDate = DateTime.Now;
            SexPopupLstItemMale.Description = "男性";
            SexPopupLstItemMale.DisplayOrder = 1;
            SexPopupLstItemMale.IsDefault = true;
            SexPopupLstItemMale.IsSystem = true;
            SexPopupLstItemMale.ItemCode = "Male";
            SexPopupLstItemMale.ModifyDate = DateTime.Now;
            SexPopupLstItemMale.PopuplstID = SexPopuplst.PopuplstID;
            SexPopupLstItemMale.RecordStatus = "Active";
            SexPopupLstItemMale.StringCode = "PopupLst_Sex_Male";
            entite.C_PopupLstItem.AddObject(SexPopupLstItemMale);
            //女性Female
            C_PopupLstItem SexPopupLstItemFemale = new C_PopupLstItem();
            SexPopupLstItemFemale.CreateDate = DateTime.Now;
            SexPopupLstItemFemale.Description = "女性";
            SexPopupLstItemFemale.DisplayOrder = 2;
            SexPopupLstItemFemale.IsDefault = false;
            SexPopupLstItemFemale.IsSystem = true;
            SexPopupLstItemFemale.ItemCode = "Female";
            SexPopupLstItemFemale.ModifyDate = DateTime.Now;
            SexPopupLstItemFemale.PopuplstID = SexPopuplst.PopuplstID;
            SexPopupLstItemFemale.RecordStatus = "Active";
            SexPopupLstItemFemale.StringCode = "PopupLst_Sex_Female";
            entite.C_PopupLstItem.AddObject(SexPopupLstItemFemale);

            entite.SaveChanges();
        }
    }
    /// <summary>
    /// 语言
    /// </summary>
    public static void AddPopuplstDataLanguage()
    {
        using (Entities entite = new Entities())
        {
            //语言
            C_Popuplst LanguagePopuplst = new C_Popuplst();
            LanguagePopuplst.CreateDate = DateTime.Now;
            LanguagePopuplst.Description = "语言";
            LanguagePopuplst.DisplayOrder = entite.C_Popuplst.Max(p => p.DisplayOrder) + 1;
            LanguagePopuplst.IsSystem = true;
            LanguagePopuplst.ModifyDate = DateTime.Now;
            LanguagePopuplst.PopuplstType = "Language";
            LanguagePopuplst.RecordStatus = "Active";
            LanguagePopuplst.StringCode = "Popuplst_Language";
            entite.C_Popuplst.AddObject(LanguagePopuplst);

            CultureInfo[] Cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            for (int i = 0; i < Cultures.Length; i++)
            {
                C_PopupLstItem LanguagePopupLstItem = new C_PopupLstItem();
                LanguagePopupLstItem.CreateDate = DateTime.Now;
                LanguagePopupLstItem.Description = Cultures[i].DisplayName;
                LanguagePopupLstItem.DisplayOrder = i + 1;
                LanguagePopupLstItem.IsDefault = Cultures[i].Name == "zh-CN" ? true : false;
                LanguagePopupLstItem.IsSystem = true;
                LanguagePopupLstItem.ItemCode = Cultures[i].Name;
                LanguagePopupLstItem.ModifyDate = DateTime.Now;
                LanguagePopupLstItem.PopuplstID = LanguagePopuplst.PopuplstID;
                LanguagePopupLstItem.RecordStatus = "Active";
                LanguagePopupLstItem.StringCode = "PopupLst_Language_" + Cultures[i].Name;
                entite.C_PopupLstItem.AddObject(LanguagePopupLstItem);
            }

            entite.SaveChanges();
        }
    }
    /// <summary>
    /// 弹出方式
    /// </summary>
    public static void AddPopuplstTarget()
    {
        using (Entities entite = new Entities())
        {
            C_Popuplst TargetPopuplst = new C_Popuplst();
            TargetPopuplst.CreateDate = DateTime.Now;
            TargetPopuplst.Description = "弹出方式";
            TargetPopuplst.DisplayOrder = entite.C_Popuplst.Max(p => p.DisplayOrder) + 1;
            TargetPopuplst.IsSystem = true;
            TargetPopuplst.ModifyDate = DateTime.Now;
            TargetPopuplst.PopuplstType = "Target";
            TargetPopuplst.RecordStatus = "Active";
            TargetPopuplst.StringCode = "Popuplst_Target";
            entite.C_Popuplst.AddObject(TargetPopuplst);

            C_PopupLstItem TargetPopupLstItem_blank = new C_PopupLstItem();
            TargetPopupLstItem_blank.CreateDate = DateTime.Now;
            TargetPopupLstItem_blank.Description = "在新的空白窗口弹出";
            TargetPopupLstItem_blank.DisplayOrder = 1;
            TargetPopupLstItem_blank.IsDefault = true;
            TargetPopupLstItem_blank.IsSystem = true;
            TargetPopupLstItem_blank.ItemCode = "_blank";
            TargetPopupLstItem_blank.ModifyDate = DateTime.Now;
            TargetPopupLstItem_blank.PopuplstID = TargetPopuplst.PopuplstID;
            TargetPopupLstItem_blank.RecordStatus = "Active";
            TargetPopupLstItem_blank.StringCode = "PopupLst_Target__blank";
            entite.C_PopupLstItem.AddObject(TargetPopupLstItem_blank);

            C_PopupLstItem TargetPopupLstItem_parent = new C_PopupLstItem();
            TargetPopupLstItem_parent.CreateDate = DateTime.Now;
            TargetPopupLstItem_parent.Description = "在父窗口弹出";
            TargetPopupLstItem_parent.DisplayOrder = 2;
            TargetPopupLstItem_parent.IsDefault = false;
            TargetPopupLstItem_parent.IsSystem = true;
            TargetPopupLstItem_parent.ItemCode = "_parent";
            TargetPopupLstItem_parent.ModifyDate = DateTime.Now;
            TargetPopupLstItem_parent.PopuplstID = TargetPopuplst.PopuplstID;
            TargetPopupLstItem_parent.RecordStatus = "Active";
            TargetPopupLstItem_parent.StringCode = "PopupLst_Target__parent";
            entite.C_PopupLstItem.AddObject(TargetPopupLstItem_parent);

            C_PopupLstItem TargetPopupLstItem_self = new C_PopupLstItem();
            TargetPopupLstItem_self.CreateDate = DateTime.Now;
            TargetPopupLstItem_self.Description = "在当前窗口弹出";
            TargetPopupLstItem_self.DisplayOrder = 3;
            TargetPopupLstItem_self.IsDefault = false;
            TargetPopupLstItem_self.IsSystem = true;
            TargetPopupLstItem_self.ItemCode = "_self";
            TargetPopupLstItem_self.ModifyDate = DateTime.Now;
            TargetPopupLstItem_self.PopuplstID = TargetPopuplst.PopuplstID;
            TargetPopupLstItem_self.RecordStatus = "Active";
            TargetPopupLstItem_self.StringCode = "PopupLst_Target__self";
            entite.C_PopupLstItem.AddObject(TargetPopupLstItem_self);

            C_PopupLstItem TargetPopupLstItem_top = new C_PopupLstItem();
            TargetPopupLstItem_top.CreateDate = DateTime.Now;
            TargetPopupLstItem_top.Description = "在顶级窗口弹出";
            TargetPopupLstItem_top.DisplayOrder = 4;
            TargetPopupLstItem_top.IsDefault = false;
            TargetPopupLstItem_top.IsSystem = true;
            TargetPopupLstItem_top.ItemCode = "_top";
            TargetPopupLstItem_top.ModifyDate = DateTime.Now;
            TargetPopupLstItem_top.PopuplstID = TargetPopuplst.PopuplstID;
            TargetPopupLstItem_top.RecordStatus = "Active";
            TargetPopupLstItem_top.StringCode = "PopupLst_Target__top";
            entite.C_PopupLstItem.AddObject(TargetPopupLstItem_top);

            entite.SaveChanges();
        }
    }

    /// <summary>
    /// 显示方式
    /// </summary>
    public static void AddPopuplstShowType()
    {
        using (Entities entite = new Entities())
        {
            C_Popuplst ShowTypePopuplst = new C_Popuplst();
            ShowTypePopuplst.CreateDate = DateTime.Now;
            ShowTypePopuplst.Description = "显示方式";
            ShowTypePopuplst.DisplayOrder = entite.C_Popuplst.Max(p => p.DisplayOrder) + 1;
            ShowTypePopuplst.IsSystem = true;
            ShowTypePopuplst.ModifyDate = DateTime.Now;
            ShowTypePopuplst.PopuplstType = "ShowType";
            ShowTypePopuplst.RecordStatus = "Active";
            ShowTypePopuplst.StringCode = "Popuplst_ShowType";
            entite.C_Popuplst.AddObject(ShowTypePopuplst);

            C_PopupLstItem ShowTypePopupLstItemText = new C_PopupLstItem();
            ShowTypePopupLstItemText.CreateDate = DateTime.Now;
            ShowTypePopupLstItemText.Description = "纯文字";
            ShowTypePopupLstItemText.DisplayOrder = 1;
            ShowTypePopupLstItemText.IsDefault = true;
            ShowTypePopupLstItemText.IsSystem = true;
            ShowTypePopupLstItemText.ItemCode = "Text";
            ShowTypePopupLstItemText.ModifyDate = DateTime.Now;
            ShowTypePopupLstItemText.PopuplstID = ShowTypePopuplst.PopuplstID;
            ShowTypePopupLstItemText.RecordStatus = "Active";
            ShowTypePopupLstItemText.StringCode = "PopupLst_ShowType_text";
            entite.C_PopupLstItem.AddObject(ShowTypePopupLstItemText);

            C_PopupLstItem ShowTypePopupLstItemIconAndText = new C_PopupLstItem();
            ShowTypePopupLstItemIconAndText.CreateDate = DateTime.Now;
            ShowTypePopupLstItemIconAndText.Description = "图标+文字";
            ShowTypePopupLstItemIconAndText.DisplayOrder = 2;
            ShowTypePopupLstItemIconAndText.IsDefault = false;
            ShowTypePopupLstItemIconAndText.IsSystem = true;
            ShowTypePopupLstItemIconAndText.ItemCode = "IconAndText";
            ShowTypePopupLstItemIconAndText.ModifyDate = DateTime.Now;
            ShowTypePopupLstItemIconAndText.PopuplstID = ShowTypePopuplst.PopuplstID;
            ShowTypePopupLstItemIconAndText.RecordStatus = "Active";
            ShowTypePopupLstItemIconAndText.StringCode = "PopupLst_ShowType_IconAndText";
            entite.C_PopupLstItem.AddObject(ShowTypePopupLstItemIconAndText);

            C_PopupLstItem ShowTypePopupLstItemImage = new C_PopupLstItem();
            ShowTypePopupLstItemImage.CreateDate = DateTime.Now;
            ShowTypePopupLstItemImage.Description = "图片";
            ShowTypePopupLstItemImage.DisplayOrder = 3;
            ShowTypePopupLstItemImage.IsDefault = false;
            ShowTypePopupLstItemImage.IsSystem = true;
            ShowTypePopupLstItemImage.ItemCode = "Image";
            ShowTypePopupLstItemImage.ModifyDate = DateTime.Now;
            ShowTypePopupLstItemImage.PopuplstID = ShowTypePopuplst.PopuplstID;
            ShowTypePopupLstItemImage.RecordStatus = "Active";
            ShowTypePopupLstItemImage.StringCode = "PopupLst_ShowType_Image";
            entite.C_PopupLstItem.AddObject(ShowTypePopupLstItemImage);

            entite.SaveChanges();
        }
    }

    /// <summary>
    /// 增加默认用户
    /// </summary>
    /// <returns></returns>
    public static C_User AddDefaultUser()
    {
        C_User user = new C_User();
        user.CreateDate = DateTime.Now;
        user.ModifyDate = DateTime.Now;
        user.Password = "amwps1129";
        user.RecordStatus = "Active";
        user.UserName = "Default";
        user.LastLoginDate = DateTime.Now;
        //user.PropertyChanging += new System.ComponentModel.PropertyChangingEventHandler(user_PropertyChanging);
        using (Entities entite = new Entities())
        {
            entite.C_User.AddObject(user);
            entite.SaveChanges();
        }
        return user;
    }

    static void user_PropertyChanging(object sender, System.ComponentModel.PropertyChangingEventArgs e)
    {
        //throw new NotImplementedException();
        C_User user = sender as C_User;
        user.Password = Common.GetMd5Hash(user.Password);
    }

    /// <summary>
    /// 增加用户信息
    /// </summary>
    /// <param name="user">用户</param>
    /// <returns></returns>
    public static C_UserInfo AddDefaultUserInfo(C_User user)
    {
        using (Entities entitie = new Entities())
        {
            C_UserInfo info = new C_UserInfo();
            info.Birthday = DateTime.Now.AddYears(-18).Date;
            info.CreateDate = DateTime.Now;
            info.Email = "liuju150@gmail.com";
            var Language = from pl in entitie.C_PopupLstItem
                           join p in entitie.C_Popuplst on pl.PopuplstID equals p.PopuplstID
                           where p.PopuplstType == "Language" && pl.IsDefault == true
                           select pl.ItemCode;
            info.Language = Language.First();
            info.ModifyDate = DateTime.Now;
            info.NickName = user.UserName;
            info.PhoneNo = "+8615011902191";
            info.RecordStatus = "Active";
            var Sex = from pi in entitie.C_PopupLstItem
                        join p in entitie.C_Popuplst on pi.PopuplstID equals p.PopuplstID
                        where p.PopuplstType == "Sex" && pi.IsDefault == true
                        select pi.PopupLstItemID;
            info.SexID = Sex.First();
            info.UserID = user.UserID;

            entitie.C_UserInfo.AddObject(info);
            entitie.SaveChanges();

            return info;
        }
    }

    /// <summary>
    /// 增加默认的分类
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public static M_UrlCategory AddDefaultCategory(C_User user)
    {
        using (Entities entitie = new Entities())
        {
            M_UrlCategory categ = new M_UrlCategory();
            categ.CategoryName = "Default";
            categ.CreateDate = DateTime.Now;
            categ.Description = "Default Category";
            categ.DisplayOrder = 1;
            categ.IsDefault = true;
            categ.ModifyDate = DateTime.Now;
            categ.RecordStatus = "Active";
            categ.UserID = user.UserID;

            entitie.M_UrlCategory.AddObject(categ);
            entitie.SaveChanges();
            return categ;
        }
    }
}